<?php

namespace App\Models;

class IntegralLogs extends Base
{

    protected $table = 'integral_logs';


    const TYPE_1 = 1;
    const TYPE_2 = 2;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag">增加</span>',
        self::TYPE_2 => '<span class="el-tag el-tag--danger">减少</span>',
    ];

    protected $fillable = [
        'id','user_id','user_name','integral','desc','type','status','created_at','updated_at'
    ];

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                })->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('user_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('created_at',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'type'=> self::TYPE_TEXT,
        ]);
        return $lists;
    }
}
